package cn.itsource.org.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.JwtUtils;
import cn.itsource.base.util.LoginData;
import cn.itsource.base.util.Payload;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.system.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @ApiOperation(value = "查询全部员工" )
    @GetMapping("/all")
    public AjaxResult selectAll(){
        List<Employee> employees = employeeService.selectAll();
        return AjaxResult.me().setResultObj(employees);
    }

    /**
     * 获取当前登录的店铺的员工
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public List<Employee> getShopEmployee(HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> loginDataPayload = JwtUtils.checkToken(token);
        LoginData loginData = loginDataPayload.getLoginData();
        Logininfo logininfo = loginData.getLogininfo();
        return employeeService.selectShopEmployee(logininfo.getId());
    }


}
